package com.attendance.www.hr.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.HolidayBean;
import com.attendance.www.hr.beans.HolidayPageBean;
import com.attendance.www.hr.dao.mapper.HolidayExtMapper;
import com.attendance.www.hr.dao.mapper.HolidayMapper;
import com.attendance.www.hr.entitys.Holiday;
import com.attendance.www.hr.entitys.HolidayExample;
import com.attendance.www.hr.service.IHolidayService;

@Service
public class HolidayServiceImpl implements IHolidayService {

	@Resource
	private HolidayMapper holidayDao;
	@Resource
	private HolidayExtMapper holidayExtDao;

	/**
	 * 查询法定节假日
	 */
	@Override
	public List<HolidayBean> getHolidayList(String holidayYear) {

		HolidayExample holidayExample = new HolidayExample();
		holidayExample.createCriteria().andHolidayYearEqualTo(Integer.parseInt(holidayYear));
		List<Holiday> holidayList = holidayDao.selectByExample(holidayExample);

		List<HolidayBean> holidayBeanList = new ArrayList<>();

		for (Holiday holiday : holidayList) {
			HolidayBean holidayBean = new HolidayBean();
			holidayBean.setHolidayName(holiday.getHolidayName());
			holidayBean.setHolidayYear(holiday.getHolidayYear());
			holidayBean.setHolidayDate(holiday.getHolidayDate());
			holidayBean.setIsToWork(holiday.getIsToWork());
			holidayBean.setIsLegal(holiday.getIsLegal());
			holidayBeanList.add(holidayBean);
		}
		return holidayBeanList;
	}

	public HolidayPageBean getHolidayListByPageCount(HolidayBean holidayBean, Integer startNum, Integer pageCount) {
		List<HolidayBean> holidayBeanList = holidayExtDao.getHolidayList(holidayBean, startNum, pageCount);
		Integer pageTotal = holidayExtDao.getHolidayListPageTotal(holidayBean);
		HolidayPageBean  holidayPageBean = new HolidayPageBean();
		holidayPageBean.setHolidayBeanList(holidayBeanList);
		holidayPageBean.setPageTotal(pageTotal);
		return holidayPageBean;
	}
		
	@Override
	public boolean saveHoliday(HolidayBean holidayBean) {
		Holiday holiday=new Holiday();
		holiday.setHolidayName(holidayBean.getHolidayName());
		holiday.setHolidayYear(holidayBean.getHolidayYear());
		holiday.setIsToWork(holidayBean.getIsToWork());
		holiday.setIsLegal(holidayBean.getIsLegal());
		holiday.setHolidayDate(holidayBean.getHolidayDate());
		int record = holidayDao.updateByPrimaryKeySelective(holiday);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	@Override
	public boolean updateHoliday(HolidayBean holidayBean) {
		Holiday holiday=new Holiday();
		holiday.setHolidayId(holidayBean.getHolidayId());
		holiday.setHolidayName(holidayBean.getHolidayName());
		holiday.setHolidayYear(holidayBean.getHolidayYear());
		holiday.setIsToWork(holidayBean.getIsToWork());
		holiday.setIsLegal(holidayBean.getIsLegal());
		holiday.setHolidayDate(holidayBean.getHolidayDate());
		holiday.setUpdateTime(new Date());
		int record = holidayDao.insertSelective(holiday);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	@Override
	public boolean deleteHoliday(Integer holidayId) {
		int record = holidayDao.deleteByPrimaryKey(holidayId);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

}
